//本页面由uniapp切片工具生成，uni-app切片-可视化设计工具(一套代码编译到7个平台iOS、Android、H5、小程序)，软件下载地址：http://www.ymznkf.com/new_view_669.htm
import app from "../../App.vue"
import {
	requst_get_orderInfoByorderId,
	requst_get_menuorder
} from '@/api/menu.js'

// 定义全局参数,控制数据加载
var _self, page = 1,
	timer = null;

export default {
	data() {
		return {
			loadingText: '',
			list_goods: uni.getStorageSync("ordermenudetail"),
			order_details_16: '共2份小计￥57.60',
			orderId: null,
			orderTimefinish: null,
			timeThreshold: 5000,
			page: 0, //当前分页页码
			apiUrl: '', //后端接口地址
			id: '', //传值使用,方便存在本地的locakStorage  
			del_id: '' //方便存在本地的locakStorage  
		}
	},
	components: {

	},
	computed: {
		groupedGoods() {
			// 根据订单时间分组商品
			const grouped = {};
			this.list_goods.forEach(item => {
				// const orderTime = Math.floor(item.menuorderTimestart / this.timeThreshold) * this.timeThreshold;
				const orderTime = item.menuorderTimestart; // 假设订单时间保存在orderTime属性中
				if (!grouped[orderTime]) {
					grouped[orderTime] = [];
				}
				grouped[orderTime].push(item);
			});
			return grouped;
		},
    sortedGroupedGoods() {
      // Sort the groupedGoods based on the time in descending order
      return Object.entries(this.groupedGoods)
        .sort((a, b) => new Date(b[0]).getTime() - new Date(a[0]).getTime())
        .reduce((acc, [key, value]) => {
          acc[key] = value;
          return acc;
        }, {});
    },
	},
	onLoad(options) {
		_self = this;

		//检查是否登录参考代码,需要用的时候，可以把注释取掉
		//if(this.checkLogin()==false){
		//	return;
		//}
		//this.getLaction();//得到gps	
		this.page = 0;

		//检测有没有传入id参数
		if (options.id != null && options.id != "") {
			this.id = options.id;
			this.orderId = options.id;
		}
		//执行初始化,需要用的时候，可以把注释取掉
		//this.Refresh("init");

	},
	onShow() {
		console.log("on show");
		//if(this.checkLogin()==false){
		//	return;
		//}
		console.log(this.orderId);
		//执行初始化,需要用的时候，可以把注释取掉
		this.Refresh("init");
	},
	onPullDownRefresh: function() {
		//下拉刷新的时候请求一次数据
		this.Refresh();
	},
	methods: {
		formatDateTime(time) {
			const options = {
				year: 'numeric',
				month: '2-digit',
				day: '2-digit',
				hour: '2-digit',
				minute: '2-digit',
				second: '2-digit',
				hour12: false,
			};

			const formattedDateTime = new Date(time).toLocaleString(undefined, options);
			return formattedDateTime;
		},
		/**
		 * banner_3处理函数
		 * 数据绑定  data-index="{{index}}" 
		 * 数据取值  var index = e.currentTarget.dataset.index; var cata = this.list_cata_list[index];
		 */
		banner_3_click: function(event) {

		},
		calculateSubtotal(group) {
			// 计算小计
			let subtotal = 0;
			group.forEach(item => {
				subtotal += parseFloat(item.menuPrice) * parseInt(item.menuNumber);
			});
			return subtotal.toFixed(2);
		},
		/**
		 * btAdd_4处理函数
		 * 数据绑定  data-index="{{index}}" 
		 * 数据取值  var index = e.currentTarget.dataset.index; var cata = this.list_cata_list[index];
		 */
		btAdd_4_click: function(event) {

			this.Refresh("btAdd_click");
		},

		/**
		 * pic_9处理函数
		 * 数据绑定  data-index="{{index}}" 
		 * 数据取值  var index = e.currentTarget.dataset.index; var cata = this.list_cata_list[index];
		 */
		pic_9_click: function(event) {

		},



		//刷新数据
		Refresh: function(_action) {
			uni.showLoading();
			requst_get_orderInfoByorderId(this.orderId).then(resorderno => {
				if (resorderno.data.code == 200) {
					uni.setStorageSync('ordersdetail', resorderno.data.data)
					this.orderTimefinish = resorderno.data.data.orderTimefinish
					requst_get_menuorder(this.orderId).then(resmenuorder => {
						if (resmenuorder.data.code == 200) {
							uni.setStorageSync('ordermenudetail', resmenuorder.data.data)
							// 隐藏导航栏加载框
							uni.hideNavigationBarLoading();
							// 停止下拉动作  
							uni.stopPullDownRefresh();

							// 隐藏加载框  
							uni.hideLoading();
							this.list_goods = resmenuorder.data.data
						}
					})
				}
			}).catch(err => {
				console.log('requst_get_customerInfo', err)
			})
		},
	}
}